﻿Imports System.Runtime.CompilerServices
Imports Microsoft.VisualBasic.CompilerServices
Imports System.IO
Imports System.Text
Imports System.Net
Imports System.Web
Imports System.Collections.Specialized
Imports Microsoft.Win32
Imports System.Drawing.Imaging
Imports Zytonic_Hotkeys
Imports Zytonic_Screenshot.ItemLibrary

Public Class FrmUploads
#Region "Functions"
    Private Declare Sub ReleaseCapture Lib "user32" ()
    Private Const WM_NCLBUTTONDOWN = &HA1
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
    Declare Function SetProcessWorkingSetSize Lib "kernel32.dll" (ByVal process As IntPtr, ByVal minimumWorkingSetSize As Integer, ByVal maximumWorkingSetSize As Integer) As Integer
    Public Shared Function CreateShortcut(ByVal Target_Path As String, ByVal Shortcut_Path As String, ByVal Shortcut_Name As String, _
   ByVal Working_Directory As String, ByVal Arguments As String, ByVal Window_Style As Integer, ByVal Icon_Num As Integer) As Object
        Dim objectValue As Object = RuntimeHelpers.GetObjectValue(Interaction.CreateObject("WScript.Shell", ""))
        Dim objectValue2 As Object = RuntimeHelpers.GetObjectValue(NewLateBinding.LateGet(objectValue, Nothing, _
        "CreateShortcut", New Object() {Shortcut_Path & "\" & Shortcut_Name & ".lnk"}, Nothing, Nothing, Nothing))
        NewLateBinding.LateSet(objectValue2, Nothing, "TargetPath", New Object() {Target_Path}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "WorkingDirectory", New Object() {Working_Directory}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "Arguments", New Object() {Arguments}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "WindowStyle", New Object() {Window_Style}, Nothing, Nothing)
        NewLateBinding.LateSet(objectValue2, Nothing, "IconLocation", New Object() {Target_Path & "," _
        & Conversions.ToString(Icon_Num)}, Nothing, Nothing)
        NewLateBinding.LateCall(objectValue2, Nothing, "Save", New Object() {}, Nothing, Nothing, Nothing, True)
        Return True
    End Function
#End Region
#Region "Theme"
    Dim LeftDown As Boolean = False
    Private Sub TopBar_MouseDown(sender As Object, e As MouseEventArgs) Handles TopBar.MouseDown
        LeftDown = True
    End Sub
    Private Sub TopBar_MouseUp(sender As Object, e As MouseEventArgs) Handles TopBar.MouseUp
        LeftDown = False
    End Sub
    Private Sub TopBar_MouseLeave(sender As Object, e As EventArgs) Handles TopBar.MouseLeave
        LeftDown = False
    End Sub
    Private Sub TopBar_MouseMove(sender As Object, e As MouseEventArgs) Handles TopBar.MouseMove
        If LeftDown = True Then
            Dim lHwnd As Int32
            lHwnd = Me.Handle
            SendMessage(lHwnd, WM_NCLBUTTONDOWN, 2, 0&)
            ReleaseCapture()
        End If
    End Sub
    Private Sub Close_MouseDown(sender As Object, e As MouseEventArgs) Handles CloseCMD.MouseDown
        CloseCMD.BackgroundImage = My.Resources.X_Down
    End Sub
    Private Sub Close_MouseUp(sender As Object, e As MouseEventArgs) Handles CloseCMD.MouseUp
        CloseCMD.BackgroundImage = My.Resources.X_Ip
    End Sub
    Private Sub Minimize_MouseDown(sender As Object, e As MouseEventArgs) Handles Minimize.MouseDown
        Minimize.BackgroundImage = My.Resources.Min_Down
    End Sub
    Private Sub Minimize_MouseUp(sender As Object, e As MouseEventArgs) Handles Minimize.MouseUp
        Minimize.BackgroundImage = My.Resources.Min_Up
    End Sub
    Private Sub Close_Click(sender As Object, e As EventArgs) Handles CloseCMD.Click
        Me.Opacity = 0.0
        Me.ShowInTaskbar = False
    End Sub
    Private Sub Minimize_Click(sender As Object, e As EventArgs) Handles Minimize.Click
        Me.WindowState = FormWindowState.Minimized
    End Sub
    Private Sub MaximizeButton_MouseClick(sender As Object, e As MouseEventArgs)
        If Me.WindowState = FormWindowState.Maximized Then
            Me.WindowState = FormWindowState.Normal
            MaximizeButton.BackgroundImage = My.Resources.Max_Up
        Else
            Me.WindowState = FormWindowState.Maximized
            MaximizeButton.BackgroundImage = My.Resources.Max2_Up
        End If
    End Sub
    Private Sub MaximizeButton_MouseDown(sender As Object, e As MouseEventArgs)
        If Me.WindowState = FormWindowState.Maximized Then
            MaximizeButton.BackgroundImage = My.Resources.Max_Down
        Else
            MaximizeButton.BackgroundImage = My.Resources.Max2_Down
        End If
    End Sub
    Private Sub MaximizeButton_MouseHover(sender As Object, e As EventArgs)
        If Me.WindowState = FormWindowState.Maximized Then
            MaximizeButton.BackgroundImage = My.Resources.Max_Over
        Else
            MaximizeButton.BackgroundImage = My.Resources.Max2_Over
        End If

    End Sub
    Private Sub MaximizeButton_MouseLeave(sender As Object, e As EventArgs)

        If Me.WindowState = FormWindowState.Maximized Then
            MaximizeButton.BackgroundImage = My.Resources.Max_Up
        Else
            MaximizeButton.BackgroundImage = My.Resources.Max2_Up
        End If
    End Sub
    Private Sub Minimize_MouseHover(sender As Object, e As EventArgs)
        Minimize.BackgroundImage = My.Resources.Min_Over
    End Sub
    Private Sub Close_MouseHover(sender As Object, e As EventArgs) Handles CloseCMD.MouseHover
        CloseCMD.BackgroundImage = My.Resources.X_Over
    End Sub
    Private Sub Minimize_MouseLeave(sender As Object, e As EventArgs) Handles Minimize.MouseHover, Minimize.MouseLeave
        Minimize.BackgroundImage = My.Resources.Min_Up
    End Sub
    Private Sub Close_MouseLeave(sender As Object, e As EventArgs) Handles CloseCMD.MouseLeave
        CloseCMD.BackgroundImage = My.Resources.X_Ip
    End Sub
#End Region
    Dim Hotkeys As New Keymap
    Dim Preview As ImagePreivew
    Public Settings As New ItemDB(My.Computer.FileSystem.SpecialDirectories.Temp & "\Zytonic Screenshot_Settings.cfg")
    Private Sub FrmUploads_Load(sender As Object, e As EventArgs) Handles Me.Load
        Hotkeys.RegisterMethod("Take_Screenshot", Me)
        Hotkeys.RegisterMethod("Cancel_Screenshot", Me)
        CreateSettings()
    End Sub
    Private Sub CreateSettings()
        If Settings.Items.Count = 0 Then
            Settings.AddItem(New DB_Item("Upload Location", New List(Of String) From {"Imgur"}))
            Settings.AddItem(New DB_Item("Show Animation", New List(Of String) From {"False"}))
            Settings.AddItem(New DB_Item("Auto Upload", New List(Of String) From {"True"}))
        End If
        If Hotkeys.HotkeyDB.Items.Count = 0 Then
            Hotkeys.HotkeyDB.AddItem(New Zytonic_Hotkeys.ItemLibrary.DB_Item("27", New List(Of String) From {"Cancel_Screenshot"}))
            Hotkeys.HotkeyDB.AddItem(New Zytonic_Hotkeys.ItemLibrary.DB_Item("44", New List(Of String) From {"Take_Screenshot"}))
        End If
        FrmSettings.Show()
    End Sub
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            GC.Collect()
            GC.WaitForPendingFinalizers()
            SetProcessWorkingSetSize(Me.Handle, -1, -1)
        Catch ex As Exception
        End Try
    End Sub
    Private Sub NotifyIcon1_MouseDoubleClick(sender As Object, e As MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
        Me.Opacity = 1.0
        Me.ShowInTaskbar = True
    End Sub
    Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ExitToolStripMenuItem.Click
        Me.Close()
    End Sub
    Public Sub Take_Screenshot()
        Dim bmpScreenShot As Bitmap
        Dim gfxScreenshot As Graphics

        bmpScreenShot = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format32bppArgb)

        gfxScreenshot = Graphics.FromImage(bmpScreenShot)
        gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)

        My.Computer.Clipboard.SetImage(bmpScreenShot)
        SelectionTool.Show()
    End Sub
    Public Sub Cancel_Screenshot()
        Try
            SelectionTool.Selection.Close()
        Catch ex As Exception
        End Try
        Try
            SelectionTool.SelectionBorder.Close()
        Catch ex As Exception
        End Try
        Try
            SelectionTool.Close()
        Catch ex As Exception
        End Try
    End Sub
    Private Sub MyUploadsToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles MyUploadsToolStripMenuItem.Click
        If My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Screenshots\") Then
            Process.Start("explorer.exe", My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Screenshots\")
        Else
            MkDir(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Screenshots\")
            Process.Start("explorer.exe", My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Screenshots\")
        End If
    End Sub
    Private Sub GalleryThread()
        Dim di As New IO.DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyPictures & "\Screenshots\")
        Dim diar1 As IO.FileInfo() = di.GetFiles()
        Dim dra As IO.FileInfo

        For Each dra In diar1
            CreateGalleryItem(dra.FullName)
            Threading.Thread.Sleep(250)
        Next
    End Sub
    Private Sub CreateGalleryItem(ByVal Filename As String)
        If Me.InvokeRequired = True Then
            Me.Invoke(New Action(Of String)(AddressOf CreateGalleryItem), {Filename})
        Else
            Dim GalleryItem As New ImagePreivew(Filename, "Gallery")
        End If
    End Sub
    Private Sub InteractiveGalleryToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles InteractiveGalleryToolStripMenuItem.Click
        FrmSettings.Opacity = 1.0
        FrmSettings.ShowInTaskbar = True
    End Sub
End Class